package com.global.college.adapter;

import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.global.college.R;
import com.global.college.data.net.CourseVideosBean;
import com.global.college.ui.activity.CourseDetailActivity;
import com.global.college.utils.StringUtils;

import java.util.List;

/**
 * @author: xjh
 * @datetime: 2021/12/7
 */
public class CourseExpandAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private static String TAG = "TrainExpandAdapter";
    public static final int TYPE_COURSE_0 = 0;
    public static final int TYPE_COURSE_1 = 1;
    public static final int TYPE_VIDEO = 2;

    private String currentVideoId = "";
    private boolean isFirstLoad =true;//首次全部展开

    public CourseExpandAdapter(List data) {
        super(data);
        addItemType(TYPE_COURSE_0, R.layout.item_course_first);
        addItemType(TYPE_COURSE_1, R.layout.item_course_second);
        addItemType(TYPE_VIDEO, R.layout.item_course_third);
    }

    public void setCurrentVideoId(String videoId) {
        currentVideoId = videoId;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder holder, MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_COURSE_0:
                CourseVideosBean item0 = (CourseVideosBean) item;
//                Log.i(TAG,"第一级点击："+item0.getTrainingClassList().size()+","+item0.getClassModelList().size());
                holder.setText(R.id.tv_course_first, item0.getCatalogName());
                ImageView ivExpand = holder.getView(R.id.iv_expand);
                if (item0.isExpanded()) {
                    ivExpand.setImageResource(R.mipmap.icon_course_expand_up);
                } else {
                    ivExpand.setImageResource(R.mipmap.icon_course_expand_down);
                    if (isFirstLoad){
                        new Handler().postDelayed(() -> expand(holder.getAdapterPosition()), 50);//毫秒
                    }
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isFirstLoad  = false;
                        int pos = holder.getAdapterPosition();
                        if (item0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_COURSE_1:
                CourseVideosBean.CatalogModelListBean item1 = ((CourseVideosBean.CatalogModelListBean) item);
                holder.setText(R.id.tv_course_second, item1.getCatalogName());
                if (isFirstLoad && !item1.isExpanded()){
                    new Handler().postDelayed(() -> expand(holder.getAdapterPosition()), 50);//毫秒
                }
//                holder.setText(R.id.tv_class_num,item1.);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isFirstLoad  = false;
                        int pos = holder.getAdapterPosition();
                        if (item1.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_VIDEO:
                View view_cou_third = holder.getView(R.id.view_cou_third);
                ImageView img_course_third = holder.getView(R.id.img_course_third);
                TextView tvThird = holder.getView(R.id.tv_course_third);
                TextView tv_course_third_time = holder.getView(R.id.tv_course_third_time);
                tvThird.setTextColor(mContext.getResources().getColor(R.color.color_828A99));
                tv_course_third_time.setTextColor(mContext.getResources().getColor(R.color.color_828A99));
//                tvThird.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.mipmap.course_play), null, null, null);
                img_course_third.setImageResource(R.mipmap.course_play);
                view_cou_third.setBackgroundResource(R.drawable.shape_view_cou_third_defult);
                CourseVideosBean.CatalogModelListBean.VideoModelBean item2 = (CourseVideosBean.CatalogModelListBean.VideoModelBean) item;
                if (item2.getVideoId().equals(currentVideoId)) {
                    tvThird.setTextColor(mContext.getResources().getColor(R.color.color_2E68F2));
                    tv_course_third_time.setTextColor(mContext.getResources().getColor(R.color.color_2E68F2));
//                    tvThird.setCompoundDrawablesWithIntrinsicBounds(mContext.getDrawable(R.mipmap.icon_play_now), null, null, null);
                    img_course_third.setImageResource(R.mipmap.icon_play_now);
                    view_cou_third.setBackgroundResource(R.drawable.shape_view_cou_third);
                }
//                tvThird.setText(item2.getVideoName()+" ("+((int)item2.getVideoDuration()/60)+"分钟)");
                tvThird.setText(item2.getVideoName()+"");
                tv_course_third_time.setText(StringUtils.timeConverter(item2.getVideoDuration()));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CourseDetailActivity) mContext).getVideoAndPlay(item2.getVideoId());
                    }
                });
                break;
            default:
                break;
        }
    }

}
